home *** CD-ROM | disk | FTP | other *** search
- Path: po.CWRU.Edu!mab22
- From: mab22@po.CWRU.Edu (Michael A. Balfour)
- Newsgroups: comp.lang.c
- Subject: Perfect Hashing
- Date: 21 Feb 1996 00:30:18 GMT
- Organization: Case Western Reserve University, Cleveland, OH (USA)
- Message-ID: <4gdp2q$hd4@madeline.INS.CWRU.Edu>
- Reply-To: mab22@po.CWRU.Edu (Michael A. Balfour)
- NNTP-Posting-Host: kanga.ins.cwru.edu
-
-
- Hi!
-
- I'm looking for a perfect hashing function.
-
- That being said, let me explain a little further before the "You can't
- have a compressive perfect hashing function over all strings" starts.
-
- I'm reading in a series of strings (among other things) from sets of
- files. I'd like to perfectly hash all of the strings for each file.
- Each string is a max of 50 characters, and each file contains from
- 1-3000 strings or so, and each string is guaranteed to be unique. It
- seems that perfect hashing should be possible given that I have the
- entire set of strings to work on at one time, but on the other hand I
- only have this set available at runtime. My idea was to have a hashing
- function similar to:
-
- long hash(char *string,long magicNumber,long numberOfStrings)
- {
- long i=0;
- char *p;
-
- for (p=string;*p!='\0';++p)
- i+=(*p)*magicNumber;
- return (i%numberOfStrings);
- }
-
- where magicNumber would be some number computed for each file (and could
- possibly even be stored in the file), and numberOfStrings is of course
- the number of strings in the file, so that my hash function returns a
- number from 0-(numberOfStrings-1), suitable for array indexing.
-
- Will my approach work? How do I compute the magic number?
-
- The reason I'm looking for a perfect hash, as opposed to a pretty good
- hash, is so that I don't even need to store the strings in memory. I
- would just be able to index directly into an array which contains all of
- the other data I'm reading in. If this approach won't work, does
- anybody have any other suggestions?
-
- Thank you for your time,
-
- Mike Balfour
-
- --
- ----------------------------------+--------------------------------
- Mike Balfour, Partner | BS/MS Graduate - ECMP
- Overload Engineering | Case Western Reserve University
- "New Ideas for a Brighter Future" | Cleveland, OH
-